///*
// * $Id: AddPhysicalSignature.java,v 1.1 2014/01/23 00:12:19 jogin Exp $
// * $Name:  $
// * itext-questions@lists.sourceforge.net
// */
//package us.gaaoc.framework.webapp.backingbeans.judge;
//
//import java.io.ByteArrayInputStream;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.InputStream;
//import java.util.HashMap;
//
//import org.apache.log4j.Logger;
//
//import us.gaaoc.framework.dao.ModelFacade;
//import us.gaaoc.framework.dao.ModelFacadeHibernate;
//import us.gaaoc.framework.model.DocumentSigConfig;
//import us.gaaoc.framework.model.Documents;
//import us.gaaoc.framework.model.Person;
//import us.gaaoc.framework.util.io.FileUtility;
//import us.gaaoc.framework.util.pdf.PDFTextTargetsUtility;
//
//import com.lowagie.text.Image;
//import com.lowagie.text.pdf.PdfContentByte;
//import com.lowagie.text.pdf.PdfReader;
//import com.lowagie.text.pdf.PdfStamper;
//
///**
// * Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
// * 
// * Author: Frank Ji 03/05/2007
// */
//public class AddPhysicalSignature {
//	
//	private Logger LOG = Logger.getLogger(AddPhysicalSignature.class);
//    /**
//     * Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
//     * @param args no arguments needed
//     */
//	public byte[] readinPDFInByte;
//	public byte[] returnedPDFInByte;
//	public AddPhysicalSignature(){};
//	public ModelFacade getModelFacade() {
//		return ModelFacadeHibernate.getInstance();
//	}
//    public byte[] AddWatermark(Documents doc, Image img, Person personSigned) {
//        System.out.println("Add watermarks and pagenumbers");
//        try {  
//            String pdfToRead = doc.getTitle() + ".pdf";
//        	readinPDFInByte = doc.getContent();
//        	FileOutputStream fosFile = new FileOutputStream(pdfToRead);
//        	fosFile.write(readinPDFInByte);
//        	fosFile.flush();
//        	fosFile.close();        	
//        	
//        	LOG.debug("The pdf file is " + pdfToRead);
//        	
//        	PdfReader reader = new PdfReader(pdfToRead);
//            FileOutputStream fileOS = new FileOutputStream("ForJudgewatermarkAdded.pdf");
//           
//            // we create a stamper that will copy the document to a new file
//            PdfStamper stamp = new PdfStamper(reader, fileOS);
//        	
//            // adding some metadata
//            HashMap<String, String> moreInfo = new HashMap<String, String>();
//            moreInfo.put("Author", "Sample Judge");
//            
//            stamp.setMoreInfo(moreInfo);
//            
//            PDFTextTargetsUtility fttcu = new PDFTextTargetsUtility();
//            
//            float x_coOrdinate =0, y_coOrdinate=0;
//            
//            String target = "~"; // Must be single character currently
//            
//            InputStream in = new ByteArrayInputStream(doc.getContent());
//            float coordinates[] = fttcu.findTextTargetCoordinates(target.charAt(0),in);
//            
//            x_coOrdinate = coordinates[0];
//            y_coOrdinate = coordinates[1];
//            
//            // adding content to each page
//            int documentInstanceCode = doc.getDocumentInstance().getCode();
//            DocumentSigConfig docSigConfig = this.getModelFacade().getDocumentSigConfig(documentInstanceCode,personSigned);
//            img.setAbsolutePosition(x_coOrdinate, y_coOrdinate);
//            
//            int i = docSigConfig.getPageNumber();
//            
//            PdfContentByte under = stamp.getUnderContent(i);
//            under.addImage(img);
//            stamp.close();          
//            
//            File file = new File("ForJudgewatermarkAdded.pdf");
//			returnedPDFInByte = FileUtility.getBytesFromFile(file);  
//        }
//        catch (Exception de) {
//            de.printStackTrace();
//            LOG.debug(de);
//        }
//		return returnedPDFInByte;
//    } 
//}